package com.xm.xmapp.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xm.xmapp.R;

public class CustomLayout extends RelativeLayout {

	private TextView text1, text2;
	private ImageView img1, img2;
	public CustomLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public CustomLayout(Context context) {
		super(context);
	}

	public CustomLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		View view = LayoutInflater.from(context).inflate(R.layout.view_customlayout,
				this, true);
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.customLayout);
		text1 = (TextView) view.findViewById(R.id.text1);
		text2 = (TextView) view.findViewById(R.id.text2);
		img1 = (ImageView) view.findViewById(R.id.customLayout_img1);
		img2 = (ImageView) view.findViewById(R.id.customLayout_img2);
		RelativeLayout rel = (RelativeLayout) view.findViewById(R.id.rel);
		int bground = a.getColor(R.styleable.customLayout_bground,
				0X00FFFFFF);
		if(bground != 0X00FFFFFF)
		{
			rel.setBackgroundColor(bground);
		}
		int textColor1 = a.getColor(R.styleable.customLayout_text1Color,
				0X00FFFFFF);
		float textSize1 = a.getDimension(R.styleable.customLayout_text1Size, 0);
		String str1 = (String) a.getText(R.styleable.customLayout_text1);
		if (str1 != null)
			text1.setText(str1);
		if (textSize1 != 0) {
			text1.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize1);
		}
		if (textColor1 != 0X00FFFFFF) {
			text1.setTextColor(textColor1);
		}
		String str2 = (String) a.getText(R.styleable.customLayout_text2);
		if (str2 != null)
			text2.setText(str2);
		int textColor2 = a.getColor(R.styleable.customLayout_text2Color,
				0X00FFFFFF);
		float textSize2 = a.getDimension(R.styleable.customLayout_text2Size, 0);
		if (textSize2 != 0) {
			text2.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize2);
		}
		if (textColor2 != 0X00FFFFFF) {
			text2.setTextColor(textColor2);
		}
		Drawable drawable = a.getDrawable(R.styleable.customLayout_left);
		if (drawable != null) {
			img1.setVisibility(View.VISIBLE);
			img1.setImageDrawable(drawable);
		} else {
			img1.setVisibility(View.GONE);
		}

		drawable = a.getDrawable(R.styleable.customLayout_right);
		if (drawable != null) {
			img2.setVisibility(View.VISIBLE);
			img2.setImageDrawable(drawable);
		} else {
			img2.setVisibility(View.GONE);

		}

		drawable = a.getDrawable(R.styleable.customLayout_right_background);
		if (drawable != null) {
			text2.setBackground(drawable);
		}

		int left = (int) a.getDimension(R.styleable.customLayout_paddingLeft,
				getResources().getDimension(R.dimen.dp10));
		int top = (int) a.getDimension(R.styleable.customLayout_paddingTop,
				getResources().getDimension(R.dimen.dp10));
		int right = (int) a.getDimension(R.styleable.customLayout_paddingRight,
				getResources().getDimension(R.dimen.dp10));
		int bottom = (int) a.getDimension(R.styleable.customLayout_paddingBottom,
				getResources().getDimension(R.dimen.dp10));

		rel.setPadding(left, top, right, bottom);

		a.recycle();

	}

	public void setImg1(int resId) {

		img1.setVisibility(View.VISIBLE);
		img1.setImageResource(resId);

	}

	public void setImg1Gone() {

		img1.setVisibility(View.GONE);

	}

	public void setImg2Gone() {

		img2.setVisibility(View.GONE);

	}

	public void setText1(CharSequence text) {
		if (text != null && !text.equals(""))
			text1.setText(text);
	}
	public float getText1Size()
	{
		if(text1!=null)
			return text1.getTextSize();
		return 0;
	}
	

	public void setText2(CharSequence text) {
		if (text != null && !text.equals(""))
			text2.setText(text);
	
	}
	
	public void setText2Color(String color) {
		if (color != null && !color.equals(""))
			text2.setTextColor(Color.parseColor(color));
	
	}

	public void setImg2DrawableLeft(int resId) {

		@SuppressWarnings("deprecation")
		Drawable drawable = getResources().getDrawable(resId);
		// / 这一步必须要做,否则不会显示.
		drawable.setBounds(0, 0, drawable.getMinimumWidth(),
				drawable.getMinimumHeight());

		text2.setCompoundDrawables(drawable, null, null, null);
		
	}
}
